#!/usr/bin/env python3  /*设定脚本文件直接能执行*/
# -*- coding:utf-8 -*-   /*设置编码*/
"""
@Author: zhuo
@Software: PyCharm
@File: tQBlower.py
@Time: 2024/1/19 17:22
@Function：ctQBlower：自定义风机控件类，通过定时器实现风机转速，可设置转速。
"""

from PySide6 import QtWidgets
from PySide6.QtCore import QTimer, Qt, QRect, QSize
from PySide6.QtGui import QPixmap, QPaintEvent, QPainter
from PySide6.QtWidgets import QWidget, QVBoxLayout

RES_PATH = '../ico/'                          # 资源文件基准地址
TITLE_RESTORE_ICON = RES_PATH + "blower.png"  # 恢复图标路径


class ctQBlower(QWidget):
    """
    ctQBlower：自定义风机控件类，通过定时器实现风机转速，可设置转速。
    """

    # 初始化
    def __init__(self, msec: int = 10, rotate_angle: int = 1, speed=None):
        """
        function:  初始化
              in:  msec：定时器转动间隔，默认为10ms
                   rotate_angle：转动角度，默认为1°
                   speed：转速，默认为None
             out:  None
          return:  None
          others:  Initialize
        """
        # 继承自父类
        super(ctQBlower, self).__init__()
        # 初始角度
        self.angle = 0
        # 绘画设备的屏幕外图像表： QPixmap
        self.pixmap = None
        # 整数点精度定义二维对象的大小：QSize
        self.pixmapSize = QSize()
        # 转速
        self.speed = speed
        # 判断是否传入转速参数
        if self.speed is None:
            # 每次旋转的角度
            self.rotate_angle = rotate_angle
            # 定时器间隔
            self.msec = msec
        # 如果传入了转速参数
        else:
            # 每次旋转的角度
            self.rotate_angle = int(self.speed / 10 / 10)
            # 定时器间隔
            self.msec = 10
        # 定时器
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.__f_update)

    # 设置转速并启动函数
    def f_setSpeed(self, speed):
        """
        function:  设置转速并启动函数
              in:  speed：需要设置的转速
             out:  None
          return:  None
          others:  Set Speed And Start Func
        """
        self.f_stop()
        self.speed = speed
        # 每次旋转的角度
        self.rotate_angle = int(self.speed / 10 / 10)
        # 定时器间隔
        self.msec = 10
        self.f_start()

    # 更新绘图函数
    def __f_update(self):
        """
        function:  更新绘图函数
              in:  None
             out:  None
          return:  None
          others:  Update Plotting Func
        """
        if self.angle == 360:
            self.angle = 0
        self.angle += self.rotate_angle
        self.update()

    # 定时器启动函数
    def f_start(self):
        """
        function:  定时器启动函数
              in:  None
             out:  None
          return:  None
          others:  Timer Start Func
        """
        self.timer.start(self.msec)

    # 定时器停止函数
    def f_stop(self):
        """
        function:  定时器停止函数
              in:  None
             out:  None
          return:  None
          others:  Timer Stop Func
        """
        # 定时器停止
        self.timer.stop()

    # 设置绘图图像函数
    def setPixmap(self, pixmap: QPixmap):
        """
        function:  设置绘图图像函数
              in:  pixmap
             out:  None
          return:  None
          others:  Sets The Drawing Image Func
        """
        self.pixmap = pixmap
        self.update()

    # 设置绘图图像大小函数
    def setPixmapSize(self, size: QSize):
        """
        function:  设置绘图图像大小函数
              in:  size:QSize
             out:  None
          return:  None
          others:  Sets The Drawing Image Func
        """
        self.pixmapSize = size
        self.pixmap.scaled(self.pixmapSize, Qt.AspectRatioMode.KeepAspectRatioByExpanding)
        self.update()

    # 重写绘图函数
    def paintEvent(self, event: QPaintEvent):
        """
        function:  重写绘图函数
              in:  event:QPaintEvent
             out:  None
          return:  None
          others:  Rewrite Drawing Func
        """
        # 取x轴、y轴的中心点
        x_center, y_center = self.width() / 2, self.height() / 2
        painter = QPainter(self)
        painter.translate(x_center, y_center)
        painter.setRenderHints(QPainter.RenderHint.SmoothPixmapTransform | QPainter.RenderHint.Antialiasing, True)
        painter.rotate(self.angle)
        painter.drawPixmap(QRect(-max(x_center, y_center) / 2,
                                 -max(x_center, y_center) / 2,
                                 max(x_center, y_center),
                                 max(x_center, y_center)), self.pixmap)


if __name__ == '__main__':
    app = QtWidgets.QApplication()
    window = QtWidgets.QWidget()
    window.resize(500, 500)
    lay = QVBoxLayout()
    window.setLayout(lay)
    win = ctQBlower(msec=10, rotate_angle=1, speed=200)
    win.f_start()
    lay.addWidget(win)
    win.setPixmap(QPixmap(TITLE_RESTORE_ICON))
    window.show()
    app.exec()
